
<!DOCTYPE HTML>
<html lang="zh-hans" >
    <head>
        <meta charset="UTF-8">
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <title>第二节 制作数据集 · Tensorflow学习笔记</title>
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="description" content="">
        <meta name="generator" content="GitBook 3.2.3">
        <meta name="author" content="scottdu">
        
        
    
    <link rel="stylesheet" href="../gitbook/style.css">

    
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-katex/katex.min.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-expandable-chapters-small/expandable-chapters-small.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-tbfed-pagefooter/footer.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-alerts/style.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-donate/plugin.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-splitter/splitter.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-highlight/website.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-search/search.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-fontsettings/website.css">
                
            
        

    

    
        
    
        
    
        
    
        
    
        
    
        
    

        
    
    
    
    
    <meta name="HandheldFriendly" content="true"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../gitbook/images/apple-touch-icon-precomposed-152.png">
    <link rel="shortcut icon" href="../gitbook/images/favicon.ico" type="image/x-icon">

    
    <link rel="next" href="../chapter5/" />
    
    
    <link rel="prev" href="section4.1.html" />
    

    
    <link rel="stylesheet" href="../gitbook/gitbook-plugin-chart/c3/c3.min.css">
    <script src="../gitbook/gitbook-plugin-chart/c3/d3.min.js"></script>
    <script src="../gitbook/gitbook-plugin-chart/c3/c3.min.js"></script>
    

    <script src="../gitbook/gitbook-plugin-graph/d3.min.js"></script>
    <script src="../gitbook/gitbook-plugin-graph/function-plot.js"></script>    

    </head>
    <body>
        
<div class="book">
    <div class="book-summary">
        
            
<div id="book-search-input" role="search">
    <input type="text" placeholder="输入并搜索" />
</div>

            
                <nav role="navigation">
                


<ul class="summary">
    
    

    

    
        
        
    
        <li class="chapter " data-level="1.1" data-path="../">
            
                <a href="../">
            
                    
                    简介
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2" data-path="../chapter1/">
            
                <a href="../chapter1/">
            
                    
                    第一章 Tensorflow框架
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.2.1" data-path="../chapter1/section1.1.html">
            
                <a href="../chapter1/section1.1.html">
            
                    
                    第一节 张量、计算图、会话
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.2" data-path="../chapter1/section1.2.html">
            
                <a href="../chapter1/section1.2.html">
            
                    
                    第二节 前向传播
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.3" data-path="../chapter1/section1.3.html">
            
                <a href="../chapter1/section1.3.html">
            
                    
                    第三节 反向传播
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.4" data-path="../chapter1/section1.4.html">
            
                <a href="../chapter1/section1.4.html">
            
                    
                    第四节 搭建神经网络的步骤
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.3" data-path="../chapter2/">
            
                <a href="../chapter2/">
            
                    
                    第二章 神经网络优化
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.3.1" data-path="../chapter2/section2.1.html">
            
                <a href="../chapter2/section2.1.html">
            
                    
                    第一节 损失函数
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.2" data-path="../chapter2/section2.2.html">
            
                <a href="../chapter2/section2.2.html">
            
                    
                    第二节 学习率
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.3" data-path="../chapter2/section2.3.html">
            
                <a href="../chapter2/section2.3.html">
            
                    
                    第三节 滑动平均
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.4" data-path="../chapter2/section2.4.html">
            
                <a href="../chapter2/section2.4.html">
            
                    
                    第四节 正则化
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.5" data-path="../chapter2/section2.5.html">
            
                <a href="../chapter2/section2.5.html">
            
                    
                    第五节 神经网络的搭建
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.4" data-path="../chapter3/">
            
                <a href="../chapter3/">
            
                    
                    第三章 全连接网络基础
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.4.1" data-path="../chapter3/section3.1.html">
            
                <a href="../chapter3/section3.1.html">
            
                    
                    第一节 MINIST数据
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.2" data-path="../chapter3/section3.2.html">
            
                <a href="../chapter3/section3.2.html">
            
                    
                    第二节 模块化搭建神经网络方法
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.3" data-path="../chapter3/section3.3.html">
            
                <a href="../chapter3/section3.3.html">
            
                    
                    第三节 手写数字识别准确率输出
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.5" data-path="./">
            
                <a href="./">
            
                    
                    第四章 全连接网络实践
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.5.1" data-path="section4.1.html">
            
                <a href="section4.1.html">
            
                    
                    第一节 输入手写数字图片输出识别结果
            
                </a>
            

            
        </li>
    
        <li class="chapter active" data-level="1.5.2" data-path="section4.2.html">
            
                <a href="section4.2.html">
            
                    
                    第二节 制作数据集
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.6" data-path="../chapter5/">
            
                <a href="../chapter5/">
            
                    
                    第五章 卷积网络基础
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.6.1" data-path="../chapter5/section5.1.html">
            
                <a href="../chapter5/section5.1.html">
            
                    
                    第一节 卷积神经网络
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.2" data-path="../chapter5/section5.2.html">
            
                <a href="../chapter5/section5.2.html">
            
                    
                    第二节 lenel5代码讲解
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.7" data-path="../chapter6/">
            
                <a href="../chapter6/">
            
                    
                    第六章 卷积网络实践
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.7.1" data-path="../chapter5/section6.1.html">
            
                <a href="../chapter5/section6.1.html">
            
                    
                    第一节 复现已有的卷积神经网络
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7.2" data-path="../chapter6/section6.2.html">
            
                <a href="../chapter6/section6.2.html">
            
                    
                    第二节 用vgg16实现图片识别
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.8" data-path="../chapter7/">
            
                <a href="../chapter7/">
            
                    
                    第七章 Tensorflow应用
            
                </a>
            

            
        </li>
    

    

    <li class="divider"></li>

    <li>
        <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
            本书使用 GitBook 发布
        </a>
    </li>
</ul>


                </nav>
            
        
    </div>

    <div class="book-body">
        
            <div class="body-inner">
                
                    

<div class="book-header" role="navigation">
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href=".." >第二节 制作数据集</a>
    </h1>
</div>




                    <div class="page-wrapper" tabindex="-1" role="main">
                        <div class="page-inner">
                            
<div id="book-search-results">
    <div class="search-noresults">
    
                                <section class="normal markdown-section">
                                
                                <h1 id="&#x7B2C;&#x4E8C;&#x8282;-&#x5236;&#x4F5C;&#x6570;&#x636E;&#x96C6;">&#x7B2C;&#x4E8C;&#x8282; &#x5236;&#x4F5C;&#x6570;&#x636E;&#x96C6;</h1>
<h2 id="&#x6570;&#x636E;&#x96C6;&#x751F;&#x6210;&#x8BFB;&#x53D6;&#x6587;&#x4EF6;mnistgeneratedspy">&#x6570;&#x636E;&#x96C6;&#x751F;&#x6210;&#x8BFB;&#x53D6;&#x6587;&#x4EF6;(mnist_generateds.py)</h2>
<h3 id="&#x6570;&#x636E;&#x96C6;&#x751F;&#x6210;&#x8BFB;&#x53D6;&#x6587;&#x4EF6;mnistgeneratedspy">&#x6570;&#x636E;&#x96C6;&#x751F;&#x6210;&#x8BFB;&#x53D6;&#x6587;&#x4EF6;(<code>mnist_generateds.py</code>)</h3>
<ul>
<li><code>tfrecords</code>&#x6587;&#x4EF6;</li>
</ul>
<p>1) <code>tfrecords</code>&#xFF1A;&#x662F;&#x4E00;&#x79CD;&#x4E8C;&#x8FDB;&#x5236;&#x6587;&#x4EF6;&#xFF0C;&#x53EF;&#x5148;&#x5C06;&#x56FE;&#x7247;&#x548C;&#x6807;&#x7B7E;&#x5236;&#x4F5C;&#x6210;&#x8BE5;&#x683C;&#x5F0F;&#x7684;&#x6587;&#x4EF6;&#x3002;&#x4F7F;&#x7528; <code>tfrecords</code>&#x8FDB;&#x884C;&#x6570;&#x636E;&#x8BFB;&#x53D6;&#xFF0C;&#x4F1A;&#x63D0;&#x9AD8;&#x5185;&#x5B58;&#x5229;&#x7528;&#x7387;&#x3002;</p>
<p>2)<code>tf.train.Example</code>&#xFF1A;&#x7528;&#x6765;&#x5B58;&#x50A8;&#x8BAD;&#x7EC3;&#x6570;&#x636E;&#x3002;&#x8BAD;&#x7EC3;&#x6570;&#x636E;&#x7684;&#x7279;&#x5F81;&#x7528;&#x952E;&#x503C;&#x5BF9;&#x7684;&#x5F62;&#x5F0F;&#x8868;&#x793A;&#x3002;</p>
<p>&#x5982;&#xFF1A;<code>img_raw</code>: &#x503C; <code>label</code>:&#x503C; &#x503C;&#x662F; <code>Byteslist</code>/<code>FloatList</code>/<code>Int64List</code></p>
<p>3)<code>SerializeToString()</code>&#xFF1A;&#x628A;&#x6570;&#x636E;&#x5E8F;&#x5217;&#x5316;&#x6210;&#x5B57;&#x7B26;&#x4E32;&#x5B58;&#x50A8;&#x3002;</p>
<ul>
<li>&#x751F;&#x6210;<code>tfrecords</code>&#x6587;&#x4EF6;</li>
</ul>
<p>&#x5177;&#x4F53;&#x4EE3;&#x7801;&#xFF1A;</p>
<pre><code class="lang-python"><span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">write_tfRecord</span><span class="hljs-params">(tfRecordName, image_path, label_path)</span>:</span>
  writer = tf.python_io.TFRecordWriteer(tfRecordName)
  num_pic= <span class="hljs-number">0</span>
  f = open(label_path, <span class="hljs-string">&apos;r&apos;</span>)
  contents = f.readlines()
  f.close()
  <span class="hljs-keyword">for</span> content <span class="hljs-keyword">in</span> contents:
    value = content.split()
    img_path = image_path + value[<span class="hljs-number">0</span>]
    img = Image.open(img_path)
    img_raw = img.tobytes()
    labels = [<span class="hljs-number">0</span>] * <span class="hljs-number">10</span>
    labels[int(value[<span class="hljs-number">1</span>])] = <span class="hljs-number">1</span>

    example = tf.train.Example(features=tf.train.Features(feature={<span class="hljs-string">&apos;img_raw&apos;</span>: tf.train.Feature(bytes_list=tf.train.BytesList(value=[img_raw])),<span class="hljs-string">&apos;label&apos;</span>: tf.train.Feature(int64_list=tf.train.Int64List(value=labels))}))
    writer.write(example.SerializerToString())
    num_pic += <span class="hljs-number">1</span>
    print(<span class="hljs-string">&quot;the number of picture:&quot;</span>, num_pic)
    writer.close()
    print(<span class="hljs-string">&quot;write tfrecord successful&quot;</span>)

<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">generate_tfRecord</span><span class="hljs-params">()</span>:</span>
  isExists = os.path.exists(data_path)
  <span class="hljs-keyword">if</span> <span class="hljs-keyword">not</span> isExists:
    os.makedirs(data_path)
    print(<span class="hljs-string">&apos;The directory was created successfully&apos;</span>)
  <span class="hljs-keyword">else</span>:
    print(<span class="hljs-string">&apos;directory already exists&apos;</span>)
  write_tfRecord(tfRecord_train, image_train_path, label_train_path)
  write_tfRecord(tfRecord_test, image_test_path, label_test_path)
</code></pre>
<p>&#x6CE8;&#x89E3;:
1) &#x65B0;&#x5EFA;&#x4E00;&#x4E2A;<code>writer</code></p>
<pre><code class="lang-python">writer = tf.python_io.TFRecordWriter(tfRecordName)
</code></pre>
<p>2) <code>for</code>&#x5FAA;&#x73AF;&#x904D;&#x5386;&#x6BCF;&#x5F20;&#x56FE;&#x548C;&#x6807;&#x7B7E;</p>
<p>3) &#x628A;&#x6BCF;&#x5F20;&#x56FE;&#x7247;&#x548C;&#x6807;&#x7B7E;&#x5C01;&#x88C5;&#x5230;<code>example</code>&#x4E2D;</p>
<pre><code class="lang-python">example = tf.train.Example(features=tf.train.Features(feature={<span class="hljs-string">&apos;img_raw&apos;</span>:tf.train.Feature(bytes_list=tf.train.BytesList(value=[ img_raw])),<span class="hljs-string">&apos;label&apos;</span>:tf.train.Feature(int64_list=tf.train.Int64List(value=lab els))}))
</code></pre>
<p>4) &#x628A;<code>example</code>&#x8FDB;&#x884C;&#x5E8F;&#x5217;&#x5316;</p>
<pre><code class="lang-python">writer.write(example.SerializeToString())
</code></pre>
<p>5) &#x5173;&#x95ED;<code>writer</code></p>
<pre><code class="lang-python">writer.close()
</code></pre>
<ul>
<li>&#x89E3;&#x6790;<code>tfrecords</code>&#x6587;&#x4EF6;</li>
</ul>
<p>&#x5177;&#x4F53;&#x4EE3;&#x7801;&#xFF1A;</p>
<pre><code class="lang-python"><span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">read_tfRecord</span><span class="hljs-params">(tfRecord_path)</span>:</span>
  filename_queue = tf.train.string_input_producer([tfRecord_path])
  reader = tf.TFRecordReader()
  _, serialized_example = reader.read(filename_queue)
  features = tf.parse_single_example(serialized_example, features={<span class="hljs-string">&apos;label&apos;</span>: tf.FixedLenFeature([<span class="hljs-number">10</span>], tf.int64), <span class="hljs-string">&apos;img_raw&apos;</span>: tf.FixedLenFeature([], tf.string)})
  img = tf.decode_raw(features[<span class="hljs-string">&apos;img_raw&apos;</span>], tf.uint8)
  img.set_shape([<span class="hljs-number">784</span>])
  img = tf.cast(img, tf.float32) * &#xFF08;<span class="hljs-number">1.</span> / <span class="hljs-number">255</span>&#xFF09;
  label = tf.cast(features[<span class="hljs-string">&apos;label&apos;</span>], tf.float32)
  <span class="hljs-keyword">return</span> img, label

<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">get_tfrecord</span><span class="hljs-params">(num, isTrain=True)</span>:</span>
  <span class="hljs-keyword">if</span> isTrain:
    tfRecord_path = tfRecord_train
  <span class="hljs-keyword">else</span>:
    trRecord_path = tfRecord_test
  img, label = read_tfRecord(tfRecord_path)
  img_batch, label_batch = tf.train.shuffle_batch([img, label], batch_size = num, num_threads = <span class="hljs-number">2</span>, capacity = <span class="hljs-number">1000</span>, min_after_dequeue = <span class="hljs-number">700</span>)
  <span class="hljs-keyword">return</span> img_batch, label_batch

<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">main</span><span class="hljs-params">()</span>:</span>
  genderate_tfRecord()

<span class="hljs-keyword">if</span> __name__ == <span class="hljs-string">&apos;__main__&apos;</span>:
  main()
</code></pre>
<p>&#x6CE8;&#x89E3;&#xFF1A;</p>
<p>1) &#x4EE5;&#x4E0B;&#x51FD;&#x6570;&#x4F1A;&#x751F;&#x6210;&#x4E00;&#x4E2A;&#x5148;&#x5165;&#x5148;&#x51FA;&#x7684;&#x961F;&#x5217;&#xFF0C;&#x6587;&#x4EF6;&#x9605;&#x8BFB;&#x5668;&#x4F1A;&#x4F7F;&#x7528;&#x5B83;&#x6765;&#x8BFB;&#x53D6;&#x6570;&#x636E;</p>
<pre><code class="lang-python">filename_queue = tf.train.string_input_producer([tfRecord_path])
tf.train.string_input_producer(string_tensor, num_epochs=<span class="hljs-keyword">None</span>, shuffle=<span class="hljs-keyword">True</span>, seed=<span class="hljs-keyword">None</span>, capacity=<span class="hljs-number">32</span>, shared_name=<span class="hljs-keyword">None</span>, name=<span class="hljs-keyword">None</span>, cancel_op=<span class="hljs-keyword">None</span>)
</code></pre>
<p>&#x53C2;&#x6570;&#x8BF4;&#x660E;&#xFF1A;
<code>string_tensor</code>&#xFF1A;&#x5B58;&#x50A8;&#x56FE;&#x50CF;&#x548C;&#x6807;&#x7B7E;&#x4FE1;&#x606F;&#x7684; <code>TFRecord</code>&#x6587;&#x4EF6;&#x540D;&#x5217;&#x8868;
<code>num_epochs</code>&#xFF1A;&#x5FAA;&#x73AF;&#x8BFB;&#x53D6;&#x7684;&#x8F6E;&#x6570;(&#x53EF;&#x9009;)
<code>shuffle</code>&#xFF1A;&#x5E03;&#x5C14;&#x503C;(&#x53EF;&#x9009;)&#xFF0C;&#x5982;&#x679C;&#x4E3A;<code>True</code>&#xFF0C;&#x5219;&#x5728;&#x6BCF;&#x8F6E;&#x968F;&#x673A;&#x6253;&#x4E71;&#x8BFB;&#x53D6;&#x987A;&#x5E8F;
<code>seed</code>&#xFF1A;&#x968F;&#x673A;&#x8BFB;&#x53D6;&#x65F6;&#x8BBE;&#x7F6E;&#x7684;&#x79CD;&#x5B50;(&#x53EF;&#x9009;)
<code>capacity</code>&#xFF1A;&#x8BBE;&#x7F6E;&#x961F;&#x5217;&#x5BB9;&#x91CF;
<code>shared_name</code>&#xFF1A;(&#x53EF;&#x9009;) &#x5982;&#x679C;&#x8BBE;&#x7F6E;&#xFF0C;&#x8BE5;&#x961F;&#x5217;&#x5C06;&#x5728;&#x591A;&#x4E2A;&#x4F1A;&#x8BDD;&#x4E2D;&#x4EE5;&#x7ED9;&#x5B9A;&#x540D;&#x79F0;&#x5171;&#x4EAB;&#x3002;&#x6240;&#x6709;&#x5177;&#x6709;&#x6B64;&#x961F;&#x5217;&#x7684;&#x8BBE;&#x5907;&#x90FD;&#x53EF;&#x4EE5;&#x901A;&#x8FC7;<code>shared_name</code>&#x8BBF;&#x95EE;&#x5B83;&#x3002;&#x5728;&#x5206;&#x5E03;&#x5F0F;&#x8BBE;&#x7F6E;&#x4E2D;&#x4F7F;&#x7528;&#x8FD9;&#x79CD;&#x65B9;&#x6CD5;&#x610F;&#x5473;&#x7740;&#x6BCF;&#x4E2A;&#x540D;&#x79F0;&#x53EA;&#x80FD;&#x88AB;&#x8BBF;&#x95EE;&#x6B64;&#x64CD;&#x4F5C;&#x7684;&#x5176;&#x4E2D;&#x4E00;&#x4E2A;&#x4F1A;&#x8BDD;&#x770B;&#x5230;&#x3002;
<code>name</code>&#xFF1A;&#x64CD;&#x4F5C;&#x7684;&#x540D;&#x79F0;(&#x53EF;&#x9009;)
<code>cancel_op</code>&#xFF1A;&#x53D6;&#x6D88;&#x961F;&#x5217;(None)</p>
<p>2) &#x65B0;&#x5EFA;&#x4E00;&#x4E2A;<code>reader</code></p>
<pre><code class="lang-python">reader = tf.TFRecordReader()
</code></pre>
<p>3) &#x4EE3;&#x7801;&#x548C;&#x6CE8;&#x91CA;&#x5982;&#x4E0B;</p>
<pre><code class="lang-python">_, serialized_example = reader.read(filename_queue)
features = tf.parse_single_example(serialized_example,features={<span class="hljs-string">&apos;img_raw&apos;</span>: tf.FixedLenFeature([], tf.string),
<span class="hljs-string">&apos;label&apos;</span>: tf.FixedLenFeature([<span class="hljs-number">10</span>],tf.int64)})

<span class="hljs-comment">#&#x628A;&#x8BFB;&#x51FA;&#x7684;&#x6BCF;&#x4E2A;&#x6837;&#x672C;&#x4FDD;&#x5B58;&#x5728;serialized_example&#x4E2D;&#x8FDB;&#x884C;&#x89E3;&#x5E8F;&#x5217;&#x5316;&#xFF0C;&#x6807;&#x7B7E;&#x548C;&#x56FE;&#x7247;&#x7684;&#x952E;&#x540D;&#x5E94;&#x8BE5;&#x548C;&#x5236;&#x4F5C;tfrecords&#x7684;&#x952E;&#x540D;&#x76F8;&#x540C;&#xFF0C;&#x5176;&#x4E2D;&#x6807;&#x7B7E;&#x7ED9;&#x51FA;&#x51E0;&#x5206;&#x7C7B;&#x3002;</span>
tf.parse_single_example(serialized, features, name=<span class="hljs-keyword">None</span>, example_names=<span class="hljs-keyword">None</span>)
</code></pre>
<p>&#x8BE5;&#x51FD;&#x6570;&#x53EF;&#x4EE5;&#x5C06;<code>tf.train.Example</code>&#x534F;&#x8BAE;&#x5185;&#x5B58;&#x5757;(<code>protocol buffer</code>)&#x89E3;&#x6790;&#x4E3A;&#x5F20;&#x91CF;&#x3002;</p>
<p>&#x53C2;&#x6570;&#x8BF4;&#x660E;&#xFF1A;
<code>serialized</code>&#xFF1A;&#x4E00;&#x4E2A;&#x6807;&#x91CF;&#x5B57;&#x7B26;&#x4E32;&#x5F20;&#x91CF;
<code>features</code>&#xFF1A;&#x4E00;&#x4E2A;&#x5B57;&#x5178;&#x6620;&#x5C04;&#x529F;&#x80FD;&#x952E; <code>FixedLenFeature</code>&#x6216;<code>VarLenFeature</code>&#x503C;&#xFF0C;&#x4E5F;&#x5C31;&#x662F;&#x5728;&#x534F;&#x8BAE;&#x5185;&#x5B58;&#x5757;&#x4E2D;&#x50A8;&#x5B58;&#x7684;
<code>name</code>&#xFF1A;&#x64CD;&#x4F5C;&#x7684;&#x540D;&#x79F0;(&#x53EF;&#x9009;)
<code>example_names</code>&#xFF1A;&#x6807;&#x91CF;&#x5B57;&#x7B26;&#x4E32;&#x8054;&#x7684;&#x540D;&#x79F0;(&#x53EF;&#x9009;)</p>
<p>4) &#x5C06;<code>img_raw</code>&#x5B57;&#x7B26;&#x4E32;&#x8F6C;&#x6362;&#x4E3A;8&#x4F4D;&#x65E0;&#x7B26;&#x53F7;&#x6574;&#x578B;</p>
<pre><code class="lang-python">img = tf.decode_raw(features[<span class="hljs-string">&apos;img_raw&apos;</span>], tf.uint8)
</code></pre>
<p>5) &#x5C06;&#x5F62;&#x72B6;&#x53D8;&#x4E3A;&#x4E00;&#x884C;784&#x5217;</p>
<pre><code class="lang-python">img.set_shape([<span class="hljs-number">784</span>])
</code></pre>
<p>6) &#x53D8;&#x6210;0&#x5230;1&#x4E4B;&#x95F4;&#x7684;&#x6D6E;&#x70B9;&#x6570;</p>
<pre><code class="lang-python">img = tf.cast(img, tf.float32) * (<span class="hljs-number">1.</span> / <span class="hljs-number">255</span>)
</code></pre>
<p>7) &#x628A;&#x6807;&#x7B7E;&#x5217;&#x8868;&#x53D8;&#x4E3A;&#x6D6E;&#x70B9;&#x6570;</p>
<pre><code class="lang-python">label = tf.cast(features[<span class="hljs-string">&apos;label&apos;</span>], tf.float32)
</code></pre>
<p>8) &#x8FD4;&#x56DE;&#x56FE;&#x7247;&#x548C;&#x6807;&#x7B7E;(&#x8DF3;&#x56DE;&#x5230;<code>get_tfrecord</code>) </p>
<pre><code class="lang-python"><span class="hljs-keyword">return</span> image,label
</code></pre>
<p>9) &#x6CE8;&#x91CA;&#x5982;&#x4E0B;</p>
<pre><code class="lang-python">tf.train.shuffle_batch(tensors, batch_size, capacity, min_after_dequeue, num_threads=<span class="hljs-number">1</span>, seed=<span class="hljs-keyword">None</span>, enqueue_many=<span class="hljs-keyword">False</span>, shapes=<span class="hljs-keyword">None</span>, allow_smaller_final_batch=<span class="hljs-keyword">False</span>, shared_name=<span class="hljs-keyword">None</span>, name=<span class="hljs-keyword">None</span>)
</code></pre>
<p>&#x8FD9;&#x4E2A;&#x51FD;&#x6570;&#x968F;&#x673A;&#x8BFB;&#x53D6;&#x4E00;&#x4E2A;<code>batch</code>&#x7684;&#x6570;&#x636E;&#x3002;
&#x53C2;&#x6570;&#x8BF4;&#x660E;&#xFF1A;
<code>tensors</code>&#xFF1A;&#x5F85;&#x4E71;&#x5E8F;&#x5904;&#x7406;&#x7684;&#x5217;&#x8868;&#x4E2D;&#x7684;&#x6837;&#x672C;(&#x56FE;&#x50CF;&#x548C;&#x6807;&#x7B7E;)
<code>batch_size</code>&#xFF1A;&#x4ECE;&#x961F;&#x5217;&#x4E2D;&#x63D0;&#x53D6;&#x7684;&#x65B0;&#x6279;&#x91CF;&#x5927;&#x5C0F;
<code>capacity</code>&#xFF1A;&#x961F;&#x5217;&#x4E2D;&#x5143;&#x7D20;&#x7684;&#x6700;&#x5927;&#x6570;&#x91CF;
<code>min_after_dequeue</code>&#xFF1A;&#x51FA;&#x961F;&#x540E;&#x961F;&#x5217;&#x4E2D;&#x7684;&#x6700;&#x5C0F;&#x6570;&#x91CF;&#x5143;&#x7D20;&#xFF0C;&#x7528;&#x4E8E;&#x786E;&#x4FDD;&#x5143;&#x7D20;&#x7684;&#x6DF7;&#x5408;&#x7EA7;&#x522B;
<code>num_threads</code>&#xFF1A;&#x6392;&#x5217;<code>tensors</code>&#x7684;&#x7EBF;&#x7A0B;&#x6570;
<code>seed</code>&#xFF1A;&#x7528;&#x4E8E;&#x961F;&#x5217;&#x5185;&#x7684;&#x968F;&#x673A;&#x6D17;&#x724C;
<code>enqueue_many</code>&#xFF1A;<code>tensor</code>&#x4E2D;&#x7684;&#x6BCF;&#x4E2A;&#x5F20;&#x91CF;&#x662F;&#x5426;&#x662F;&#x4E00;&#x4E2A;&#x4F8B;&#x5B50;
<code>shapes</code>&#xFF1A;&#x6BCF;&#x4E2A;&#x793A;&#x4F8B;&#x7684;&#x5F62;&#x72B6;
<code>allow_smaller_final_batch</code>&#xFF1A;(&#x53EF;&#x9009;)&#x5E03;&#x5C14;&#x503C;&#x3002;&#x5982;&#x679C;&#x4E3A;<code>True</code>&#xFF0C;&#x5219;&#x5728;&#x961F;&#x5217;&#x4E2D;&#x5269;&#x4F59;&#x6570;&#x91CF;&#x4E0D;&#x8DB3;&#x65F6;&#x5141;&#x8BB8;&#x6700;&#x7EC8;&#x6279;&#x6B21;&#x66F4;&#x5C0F;&#x3002;
<code>shared_name</code>&#xFF1A;(&#x53EF;&#x9009;)&#x5982;&#x679C;&#x8BBE;&#x7F6E;&#xFF0C;&#x8BE5;&#x961F;&#x5217;&#x5C06;&#x5728;&#x591A;&#x4E2A;&#x4F1A;&#x8BDD;&#x4E2D;&#x4EE5;&#x7ED9;&#x5B9A;&#x540D;&#x79F0;&#x5171;&#x4EAB;&#x3002;
<code>name</code>&#xFF1A;&#x64CD;&#x4F5C;&#x7684;&#x540D;&#x79F0;(&#x53EF;&#x9009;)</p>
<p>10) &#x8FD4;&#x56DE;&#x7684;&#x56FE;&#x7247;&#x548C;&#x6807;&#x7B7E;&#x4E3A;&#x968F;&#x673A;&#x62BD;&#x53D6;&#x7684;<code>batch_size</code>&#x7EC4;</p>
<pre><code class="lang-python"><span class="hljs-keyword">return</span> img_batch,label_batch
</code></pre>
<h3 id="&#x53CD;&#x5411;&#x4F20;&#x64AD;&#x6587;&#x4EF6;&#x4FEE;&#x6539;&#x56FE;&#x7247;&#x6807;&#x7B7E;&#x83B7;&#x53D6;&#x7684;&#x63A5;&#x53E3;mnistbackwardpy">&#x53CD;&#x5411;&#x4F20;&#x64AD;&#x6587;&#x4EF6;&#x4FEE;&#x6539;&#x56FE;&#x7247;&#x6807;&#x7B7E;&#x83B7;&#x53D6;&#x7684;&#x63A5;&#x53E3;(<code>mnist_backward.py</code>)</h3>
<ul>
<li>&#x5173;&#x952E;&#x64CD;&#x4F5C;&#xFF1A;&#x5229;&#x7528;&#x591A;&#x7EBF;&#x7A0B;&#x63D0;&#x9AD8;&#x56FE;&#x7247;&#x548C;&#x6807;&#x7B7E;&#x7684;&#x6279;&#x83B7;&#x53D6;&#x6548;&#x7387;
&#x65B9;&#x6CD5;&#xFF1A;&#x5C06;&#x6279;&#x83B7;&#x53D6;&#x7684;&#x64CD;&#x4F5C;&#x653E;&#x5230;&#x7EBF;&#x7A0B;&#x534F;&#x8C03;&#x5668;&#x5F00;&#x542F;&#x548C;&#x5173;&#x95ED;&#x4E4B;&#x95F4;</li>
</ul>
<p>&#x5F00;&#x542F;&#x7EBF;&#x7A0B;&#x534F;&#x8C03;&#x5668;&#xFF1A;</p>
<pre><code class="lang-python">coord = tf.train.Coordinator()
threads = tf.train.start_queue_runners(sess=sess, coord=coord)
</code></pre>
<p>&#x5173;&#x95ED;&#x7EBF;&#x7A0B;&#x534F;&#x8C03;&#x5668;&#xFF1A;</p>
<pre><code class="lang-python">coord.request_stop()
coord.join(threads)
</code></pre>
<p>&#x6CE8;&#x89E3;:</p>
<pre><code class="lang-python">tf.train.start_queue_runners(sess=<span class="hljs-keyword">None</span>, coord=<span class="hljs-keyword">None</span>, daemon=<span class="hljs-keyword">True</span>, start=<span class="hljs-keyword">True</span>, collection=tf.GraphKeys.QUEUE_RUNNERS)
</code></pre>
<p>&#x8FD9;&#x4E2A;&#x51FD;&#x6570;&#x5C06;&#x4F1A;&#x542F;&#x52A8;&#x8F93;&#x5165;&#x961F;&#x5217;&#x7684;&#x7EBF;&#x7A0B;&#xFF0C;&#x586B;&#x5145;&#x8BAD;&#x7EC3;&#x6837;&#x672C;&#x5230;&#x961F;&#x5217;&#x4E2D;&#xFF0C;&#x4EE5;&#x4FBF;&#x51FA;&#x961F;&#x64CD;&#x4F5C;&#x53EF;&#x4EE5;&#x4ECE;&#x961F;&#x5217;&#x4E2D;&#x62FF;&#x5230;&#x6837;&#x672C;&#x3002;&#x8FD9;&#x79CD;&#x60C5;&#x51B5;&#x4E0B;&#x6700;&#x597D;&#x914D;&#x5408;&#x4F7F;&#x7528;&#x4E00;&#x4E2A;<code>tf.train.Coordinator</code>&#xFF0C;&#x8FD9;&#x6837;&#x53EF;&#x4EE5;&#x5728;&#x53D1;&#x751F;&#x9519;&#x8BEF;&#x7684;&#x60C5;&#x51B5;&#x4E0B;&#x6B63;&#x786E;&#x5730;&#x5173;&#x95ED;&#x8FD9;&#x4E9B;&#x7EBF;&#x7A0B;&#x3002;</p>
<p>&#x53C2;&#x6570;&#x8BF4;&#x660E;&#xFF1A;
<code>sess</code>&#xFF1A;&#x7528;&#x4E8E;&#x8FD0;&#x884C;&#x961F;&#x5217;&#x64CD;&#x4F5C;&#x7684;&#x4F1A;&#x8BDD;&#x3002;&#x9ED8;&#x8BA4;&#x4E3A;&#x9ED8;&#x8BA4;&#x4F1A;&#x8BDD;&#x3002;
<code>coord</code>&#xFF1A;&#x53EF;&#x9009;&#x534F;&#x8C03;&#x5668;&#xFF0C;&#x7528;&#x4E8E;&#x534F;&#x8C03;&#x542F;&#x52A8;&#x7684;&#x7EBF;&#x7A0B;&#x3002;
<code>daemon</code>&#xFF1A;&#x5B88;&#x62A4;&#x8FDB;&#x7A0B;&#xFF0C;&#x7EBF;&#x7A0B;&#x662F;&#x5426;&#x5E94;&#x8BE5;&#x6807;&#x8BB0;&#x4E3A;&#x5B88;&#x62A4;&#x8FDB;&#x7A0B;&#xFF0C;&#x8FD9;&#x610F;&#x5473;&#x7740;&#x5B83;&#x4EEC;&#x4E0D;&#x4F1A;&#x963B;&#x6B62;&#x7A0B;&#x5E8F;&#x9000;&#x51FA;&#x3002;
<code>start</code>&#xFF1A;&#x8BBE;&#x7F6E;&#x4E3A;<code>False</code>&#x53EA;&#x521B;&#x5EFA;&#x7EBF;&#x7A0B;&#xFF0C;&#x4E0D;&#x542F;&#x52A8;&#x5B83;&#x4EEC;&#x3002;
<code>collection</code>&#xFF1A;&#x6307;&#x5B9A;&#x56FE;&#x96C6;&#x5408;&#x4EE5;&#x83B7;&#x53D6;&#x542F;&#x52A8;&#x961F;&#x5217;&#x7684;<code>GraphKey</code>&#x3002;&#x9ED8;&#x8BA4;&#x4E3A;<code>GraphKeys.QUEUE_RUNNERS</code>&#x3002;</p>
<ul>
<li>&#x5177;&#x4F53;&#x5BF9;&#x6BD4;&#x53CD;&#x5411;&#x4F20;&#x64AD;&#x4E2D;&#x7684; fc4 &#x4E0E; fc3 &#x4EE3;&#x7801;</li>
</ul>
<pre><code class="lang-python"><span class="hljs-keyword">import</span> tensorflow <span class="hljs-keyword">as</span> tf
<span class="hljs-keyword">from</span> tensorflow.examples.tutorials.mnist <span class="hljs-keyword">import</span> input_data
<span class="hljs-keyword">import</span> mnist_forward
<span class="hljs-keyword">import</span> os
<span class="hljs-keyword">import</span> mnist_generateds

BATCH_SIZE = <span class="hljs-number">200</span>
LEARNING_RATE_BASE = <span class="hljs-number">0.1</span>
LEARNING_RATE_DECAY = <span class="hljs-number">0.99</span>
MODEL_SAVE_PATH = <span class="hljs-string">&apos;./model/&apos;</span>
MODEL_NAME = <span class="hljs-string">&apos;mnist_model&apos;</span>
train_num_examples = <span class="hljs-number">60000</span>

<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">backward</span><span class="hljs-params">()</span>:</span>
  x = tf.placeholder(tf.float32, [<span class="hljs-keyword">None</span>, mnist_forward.INPUT_NODE])
  y = tf.placeholder(tf.float32, [<span class="hljs-keyword">None</span>, mnist_forward.OUTPUT_NODE])
  y = mnist_forward.forward(x, REGULARIZER)
  global_step = tf.Varialbe(<span class="hljs-number">0</span>, trainable=<span class="hljs-keyword">False</span>)

  ce = tf.nn.sparse_softmax_cross_entropy_with_logits(logits=y, labels=tf.argmax(y_, <span class="hljs-number">1</span>))
  cem = tf.reduce_mean(ce)
  loss = cem + tf.add_n(tf.get_collection(<span class="hljs-string">&apos;losses&apos;</span>))

  learning_rate = tf.train.exponential_decay(LEARING_RATE_BASE, global_step, train_num_examples / BATCH_SIZE, LEARNING_RATE_DECAY, staircase=<span class="hljs-keyword">True</span>)

  train_step = tf.train.GradientDescentOptimizer(learning_rate).minimize(loss, global_step=global_step)
  ema = tf.train.ExponentialMovingAverage(MOVING_AVERAGE_DECAY, global_step)
  ema_op = ema.apply(tf.trainable_variables())
  <span class="hljs-keyword">with</span> tf.control_dependencies([train_step, ema_op]):
  train_op = tf.no_op(name=<span class="hljs-string">&apos;train&apos;</span>)

saver = tf.train.Saver()
img_batch, label_batch = mnist_generateds.get_tfrecord(BATCH_SIZE, isTrain=<span class="hljs-keyword">True</span>)

<span class="hljs-keyword">with</span> tf.Session() <span class="hljs-keyword">as</span> sess:
  init_op = tf.global_variables_initializer()
  sess.run(init_op)

  ckpt = tf.train.get_checkpoint_state(MODEL_SAVE_PATH)
  <span class="hljs-keyword">if</span> ckpt <span class="hljs-keyword">and</span> ckpt.model_checkpoint_path:
    coord = tf.train.Coordinator()
    threads = tf.train.start_queue_runners(sess=sess, coord=coord)

    <span class="hljs-keyword">for</span> i <span class="hljs-keyword">in</span> range(STEPS):
      xs, ys = sess.run([img_batch, label_batch])
      _, loss_value, step = sess.run([train_op, loss, global_step], feed_dict={x: xs, y_: ys})
      <span class="hljs-keyword">if</span> i % <span class="hljs-number">1000</span> == <span class="hljs-number">0</span>:
        print(<span class="hljs-string">&quot;After %d training step(s), loss on training batch is %g.&quot;</span> % (step, loss_value))
        saver.save(sess, os.path.join(MODEL_SAVE_PATH, MODEL_NAME), global_step=global_step)
    coord.request_stop()
    coord.join(threads)

<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">main</span><span class="hljs-params">()</span>:</span>
  backward()

<span class="hljs-keyword">if</span> __name__ == <span class="hljs-string">&apos;__main__&apos;</span>:
  main()
</code></pre>
<p>&#x6CE8;&#x89E3;:</p>
<p>1)<code>train_num_examples=60000</code>&#x5728;&#x68AF;&#x5EA6;&#x4E0B;&#x964D;&#x5B66;&#x4E60;&#x7387;&#x4E2D;&#x9700;&#x8981;&#x8BA1;&#x7B97;&#x591A;&#x5C11;&#x8F6E;&#x66F4;&#x65B0;&#x4E00;&#x6B21;&#x5B66;&#x4E60;&#x7387;&#xFF0C;&#x8FD9;&#x4E2A;&#x503C;&#x662F;&#xFF1A;<span class="katex"><span class="katex-mathml"><math><semantics><mrow><mfrac><mrow><mi>t</mi><mi>o</mi><mi>t</mi><mi>a</mi><mi>l</mi><mi mathvariant="normal">_</mi><mi>s</mi><mi>a</mi><mi>m</mi><mi>p</mi><mi>l</mi><mi>e</mi><mi mathvariant="normal">_</mi><mi>n</mi><mi>u</mi><mi>m</mi></mrow><mrow><mi>b</mi><mi>a</mi><mi>t</mi><mi>c</mi><mi>h</mi><mi mathvariant="normal">_</mi><mi>s</mi><mi>i</mi><mi>z</mi><mi>e</mi></mrow></mfrac></mrow><annotation encoding="application/x-tex">\frac{total\_sample\_num}{batch\_size}</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="strut" style="height:1.013108em;"></span><span class="strut bottom" style="height:1.5751079999999997em;vertical-align:-0.5619999999999999em;"></span><span class="base textstyle uncramped"><span class="mord reset-textstyle textstyle uncramped"><span class="mopen sizing reset-size5 size5 reset-textstyle textstyle uncramped nulldelimiter"></span><span class="mfrac"><span class="vlist"><span style="top:0.345em;"><span class="fontsize-ensurer reset-size5 size5"><span style="font-size:0em;">&#x200B;</span></span><span class="reset-textstyle scriptstyle cramped mtight"><span class="mord scriptstyle cramped mtight"><span class="mord mathit mtight">b</span><span class="mord mathit mtight">a</span><span class="mord mathit mtight">t</span><span class="mord mathit mtight">c</span><span class="mord mathit mtight">h</span><span class="mord mathrm mtight" style="margin-right:0.02778em;">_</span><span class="mord mathit mtight">s</span><span class="mord mathit mtight">i</span><span class="mord mathit mtight" style="margin-right:0.04398em;">z</span><span class="mord mathit mtight">e</span></span></span></span><span style="top:-0.22999999999999998em;"><span class="fontsize-ensurer reset-size5 size5"><span style="font-size:0em;">&#x200B;</span></span><span class="reset-textstyle textstyle uncramped frac-line"></span></span><span style="top:-0.527em;"><span class="fontsize-ensurer reset-size5 size5"><span style="font-size:0em;">&#x200B;</span></span><span class="reset-textstyle scriptstyle uncramped mtight"><span class="mord scriptstyle uncramped mtight"><span class="mord mathit mtight">t</span><span class="mord mathit mtight">o</span><span class="mord mathit mtight">t</span><span class="mord mathit mtight">a</span><span class="mord mathit mtight" style="margin-right:0.01968em;">l</span><span class="mord mathrm mtight" style="margin-right:0.02778em;">_</span><span class="mord mathit mtight">s</span><span class="mord mathit mtight">a</span><span class="mord mathit mtight">m</span><span class="mord mathit mtight">p</span><span class="mord mathit mtight" style="margin-right:0.01968em;">l</span><span class="mord mathit mtight">e</span><span class="mord mathrm mtight" style="margin-right:0.02778em;">_</span><span class="mord mathit mtight">n</span><span class="mord mathit mtight">u</span><span class="mord mathit mtight">m</span></span></span></span><span class="baseline-fix"><span class="fontsize-ensurer reset-size5 size5"><span style="font-size:0em;">&#x200B;</span></span>&#x200B;</span></span></span><span class="mclose sizing reset-size5 size5 reset-textstyle textstyle uncramped nulldelimiter"></span></span></span></span></span></p>
<p><strong>&#x4E4B;&#x524D;</strong>&#xFF1A;&#x7528;<code>mnist.train.num_examples</code>&#x8868;&#x793A;&#x603B;&#x6837;&#x672C;&#x6570;; </p>
<p><strong>&#x73B0;&#x5728;</strong>&#xFF1A;&#x8981;&#x624B;&#x52A8;&#x7ED9;&#x51FA;&#x8BAD;&#x7EC3;&#x7684;&#x603B;&#x6837;&#x672C;&#x6570;&#xFF0C;&#x8FD9;&#x4E2A;&#x6570;&#x662F;6&#x4E07;&#x3002;</p>
<p>2)<code>image_batch, label_batch=mnist_generateds.get_tfrecord(BATCH_SIZE, isTrain=True)</code></p>
<p><strong>&#x4E4B;&#x524D;</strong>&#xFF1A;&#x7528;<code>mnist.train.next_batch</code> &#x51FD;&#x6570;&#x8BFB;&#x51FA;&#x56FE;&#x7247;&#x548C;&#x6807;&#x7B7E;&#x5582;&#x7ED9;&#x7F51;&#x7EDC;; </p>
<p><strong>&#x73B0;&#x5728;</strong>&#xFF1A;&#x7528;&#x51FD;&#x6570;<code>get_tfrecord</code>&#x66FF;&#x6362;&#xFF0C;&#x4E00;&#x6B21;&#x6279;&#x83B7;&#x53D6;<code>batch_size</code>&#x5F20;&#x56FE;&#x7247;&#x548C;&#x6807;&#x7B7E;&#x3002; 
<code>isTrain</code>&#xFF1A;&#x7528;&#x6765;&#x533A;&#x5206;&#x8BAD;&#x7EC3;&#x9636;&#x6BB5;&#x548C;&#x6D4B;&#x8BD5;&#x9636;&#x6BB5;&#xFF0C;<code>True</code>&#x8868;&#x793A;&#x8BAD;&#x7EC3;&#xFF0C;<code>False</code>&#x8868;&#x793A;&#x6D4B;&#x8BD5;&#x3002;
 3)<code>xs,ys=sess.run([img_batch,label_batch])</code></p>
<p><strong>&#x4E4B;&#x524D;</strong>&#xFF1A;&#x4F7F;&#x7528;&#x51FD;&#x6570; <code>xs,ys=mnist.train.next_batch(BATCH_SIZE)</code></p>
<p><strong>&#x73B0;&#x5728;</strong>&#xFF1A;&#x5728;<code>sess.run</code>&#x4E2D;&#x6267;&#x884C;&#x56FE;&#x7247;&#x548C;&#x6807;&#x7B7E;&#x7684;&#x6279;&#x83B7;&#x53D6;&#x3002;</p>
<h3 id="&#x6D4B;&#x8BD5;&#x6587;&#x4EF6;&#x4FEE;&#x6539;&#x56FE;&#x7247;&#x6807;&#x7B7E;&#x83B7;&#x53D6;&#x7684;&#x63A5;&#x53E3;mnisttestpy">&#x6D4B;&#x8BD5;&#x6587;&#x4EF6;&#x4FEE;&#x6539;&#x56FE;&#x7247;&#x6807;&#x7B7E;&#x83B7;&#x53D6;&#x7684;&#x63A5;&#x53E3;(mnist_test.py)</h3>
<ul>
<li>&#x5177;&#x4F53;&#x5BF9;&#x6BD4;&#x53CD;&#x5411;&#x4F20;&#x64AD;&#x4E2D;&#x7684; fc4 &#x4E0E; fc3 &#x4EE3;&#x7801;(&#x548C;&#x53CD;&#x5411;&#x4F20;&#x64AD;&#x7C7B;&#x4F3C;)</li>
</ul>
<pre><code class="lang-python"><span class="hljs-comment">#coding:utf-8</span>
<span class="hljs-keyword">import</span> time
<span class="hljs-keyword">import</span> tensorflow <span class="hljs-keyword">as</span> tf
<span class="hljs-keyword">from</span> tensorflow.examples.tutorials.mnist <span class="hljs-keyword">import</span> input_data
<span class="hljs-keyword">import</span> mnist_forward
<span class="hljs-keyword">import</span> mnist_backward
<span class="hljs-keyword">import</span> mnist_generateds
TEST_INTERVAL_SECS = <span class="hljs-number">5</span>
TEST_NUM = <span class="hljs-number">10000</span>

<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">test</span><span class="hljs-params">()</span>:</span>
  <span class="hljs-keyword">with</span> tf.Graph().as_default() <span class="hljs-keyword">as</span> g:
    x = tf.placeholder(tf.float32, [<span class="hljs-keyword">None</span>, mnist_forward.INPUT_NODE])
    y_ = tf.placeholder(tf.float32, [<span class="hljs-keyword">None</span>, mnist_forward.OUTPUT_NODE])
    y = mnist_forward.forward(x, <span class="hljs-keyword">None</span>)

    ema = tf.train.ExponentialMovingAverage(mnist_backward.MOVING_AVERAGE_DECAY)
    ema_restore = ema.variables_to_restore()
    saver = tf.train.Saver(ema_restore)

    correct_prediction = tf.equal(tf.argmax(y, <span class="hljs-number">1</span>), tf.argmax(y_, <span class="hljs-number">1</span>))
    accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))

  img_batch, label_batch = mnist_generateds.get_tfrecord(TEST_NUM, isTrain=false)

  <span class="hljs-keyword">while</span> <span class="hljs-keyword">True</span>:
    <span class="hljs-keyword">with</span> tf.Session() <span class="hljs-keyword">as</span> sess:
      ckpt = tf.train.get_checkpoint_state(mnist_backward.MODEL_SAVE_PATH)
      <span class="hljs-keyword">if</span> ckpt <span class="hljs-keyword">and</span> ckpt.model_checkpoint_path:
        saver.restore(sess, ckpt.model_checkpoint_path)
        global_step = ckpt.model_checkpoint_path.split(<span class="hljs-string">&apos;/&apos;</span>)[<span class="hljs-number">-1</span>].split(<span class="hljs-string">&apos;-&apos;</span>)[<span class="hljs-number">-1</span>]

        coord = tf.train.Coordinator()
        threads = tf.train.start_queue_runners(sess=sess, coord=coord)
        xs, ys = sess.run([img_batch, label_batch])

        accuracy_score = sess.run(accuracy, feed_dict={x: xs, y_: ys})
        print(<span class="hljs-string">&quot;After %s training step(s), test accuracy = %g&quot;</span> % (global_step, accuracy_score))
      <span class="hljs-keyword">else</span>:
        print(<span class="hljs-string">&quot;No checkpoint file foud.&quot;</span>)
        <span class="hljs-keyword">return</span>
    time.sleep(TEST_INTERVAL_SECS)

<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">main</span><span class="hljs-params">()</span>:</span>
  test()

<span class="hljs-keyword">if</span> __name__ == <span class="hljs-string">&apos;__main__&apos;</span>:
  main()
</code></pre>
<p>&#x6CE8;&#x89E3;:
1)<code>TEST_NUM=10000</code></p>
<p><strong>&#x4E4B;&#x524D;</strong>&#xFF1A;&#x7528;<code>mnist.test.num_examples</code>&#x8868;&#x793A;&#x603B;&#x6837;&#x672C;&#x6570;;</p>
<p><strong>&#x73B0;&#x5728;</strong>&#xFF1A;&#x8981;&#x624B;&#x52A8;&#x7ED9;&#x51FA;&#x6D4B;&#x8BD5;&#x7684;&#x603B;&#x6837;&#x672C;&#x6570;&#xFF0C;&#x8FD9;&#x4E2A;&#x6570;&#x662F;1&#x4E07;&#x3002;</p>
<p>2)<code>image_batch,label_batch=mnist_generateds.get_tfrecord(TEST_NUM,
isTrain=False)</code></p>
<p><strong>&#x4E4B;&#x524D;</strong>&#xFF1A;&#x7528;<code>mnist.test.next_batch</code>&#x51FD;&#x6570;&#x8BFB;&#x51FA;&#x56FE;&#x7247;&#x548C;&#x6807;&#x7B7E;&#x5582;&#x7ED9;&#x7F51;&#x7EDC;;</p>
<p><strong>&#x73B0;&#x5728;</strong>&#xFF1A;&#x7528;&#x51FD;&#x6570;<code>get_tfrecord</code>&#x66FF;&#x6362;&#x8BFB;&#x53D6;&#x6240;&#x6709;&#x6D4B;&#x8BD5;&#x96C6;1&#x4E07;&#x5F20;&#x56FE;&#x7247;&#x3002;
<code>isTrain</code>:&#x7528;&#x6765;&#x533A;&#x5206;&#x8BAD;&#x7EC3;&#x9636;&#x6BB5;&#x548C;&#x6D4B;&#x8BD5;&#x9636;&#x6BB5;&#xFF0C;<code>True</code>&#x8868;&#x793A;&#x8BAD;&#x7EC3;&#xFF0C;<code>False</code>&#x8868;&#x793A;&#x6D4B;&#x8BD5;&#x3002; 3)<code>xs,ys=sess.run([img_batch,label_batch])</code></p>
<p><strong>&#x4E4B;&#x524D;</strong>&#xFF1A;&#x4F7F;&#x7528;&#x51FD;&#x6570;<code>xs,ys=mnist.test.next_batch(BATCH_SIZE)</code></p>
<p><strong>&#x73B0;&#x5728;</strong>&#xFF1A;&#x5728;<code>sess.run</code>&#x4E2D;&#x6267;&#x884C;&#x56FE;&#x7247;&#x548C;&#x6807;&#x7B7E;&#x7684;&#x6279;&#x83B7;&#x53D6;&#x3002;</p>
<h3 id="&#x5B9E;&#x8DF5;&#x4EE3;&#x7801;&#x9A8C;&#x8BC1;">&#x5B9E;&#x8DF5;&#x4EE3;&#x7801;&#x9A8C;&#x8BC1;</h3>
<p>1)&#x8FD0;&#x884C;&#x6D4B;&#x8BD5;&#x4EE3;&#x7801; mnist_test.py</p>
<p><img src="http://ovhbzkbox.bkt.clouddn.com/2018-08-15-15343080046077.jpg" alt=""></p>
<p>2)&#x51C6;&#x786E;&#x7387;&#x7A33;&#x5B9A;&#x5728; 95%&#x4EE5;&#x4E0A;&#x540E;&#x8FD0;&#x884C;&#x5E94;&#x7528;&#x7A0B;&#x5E8F; mnist_app.py</p>
<p><img src="http://ovhbzkbox.bkt.clouddn.com/2018-08-15-15343080436967.jpg" alt=""></p>
<footer class="page-footer"><span class="copyright">Copyright &#xA9; scottdu 2018 all right reserved&#xFF0C;powered by Gitbook</span><span class="footer-modification">&#x8BE5;&#x6587;&#x4EF6;&#x4FEE;&#x8BA2;&#x65F6;&#x95F4;&#xFF1A;
2018-08-18 15:53:44
</span></footer> <link rel="stylesheet" type="text/css" href="https://storage.googleapis.com/app.klipse.tech/css/codemirror.css"> <script>     window.klipse_settings = {         selector: ".language-klipse, .lang-eval-clojure",         selector_eval_js: ".lang-eval-js",         selector_eval_python_client: ".lang-eval-python",         selector_eval_php: ".lang-eval-php",         selector_eval_scheme: ".lang-eval-scheme",         selector_eval_ruby: ".lang-eval-ruby",         selector_reagent: ".lang-reagent",        selector_google_charts: ".lang-google-chart",        selector_es2017: ".lang-eval-es2017",        selector_jsx: ".lang-eval-jsx",        selector_transpile_jsx: ".lang-transpile-jsx",        selector_render_jsx: ".lang-render-jsx",        selector_react: ".lang-react",        selector_eval_markdown: ".lang-render-markdown",        selector_eval_lambdaway: ".lang-render-lambdaway",        selector_eval_cpp: ".lang-eval-cpp",        selector_eval_html: ".lang-render-html",        selector_sql: ".lang-eval-sql",        selector_brainfuck: "lang-eval-brainfuck",        selector_js: ".lang-transpile-cljs"    }; </script> <script src="https://storage.googleapis.com/app.klipse.tech/plugin/js/klipse_plugin.js"></script>
                                
                                </section>
                            
    </div>
    <div class="search-results">
        <div class="has-results">
            
            <h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
            <ul class="search-results-list"></ul>
            
        </div>
        <div class="no-results">
            
            <h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
            
        </div>
    </div>
</div>

                        </div>
                    </div>
                
            </div>

            
                
                <a href="section4.1.html" class="navigation navigation-prev " aria-label="Previous page: 第一节 输入手写数字图片输出识别结果">
                    <i class="fa fa-angle-left"></i>
                </a>
                
                
                <a href="../chapter5/" class="navigation navigation-next " aria-label="Next page: 第五章 卷积网络基础">
                    <i class="fa fa-angle-right"></i>
                </a>
                
            
        
    </div>

    <script>
        var gitbook = gitbook || [];
        gitbook.push(function() {
            gitbook.page.hasChanged({"page":{"title":"第二节 制作数据集","level":"1.5.2","depth":2,"next":{"title":"第五章 卷积网络基础","level":"1.6","depth":1,"path":"chapter5/README.md","ref":"chapter5/README.md","articles":[{"title":"第一节 卷积神经网络","level":"1.6.1","depth":2,"path":"chapter5/section5.1.md","ref":"chapter5/section5.1.md","articles":[]},{"title":"第二节 lenel5代码讲解","level":"1.6.2","depth":2,"path":"chapter5/section5.2.md","ref":"chapter5/section5.2.md","articles":[]}]},"previous":{"title":"第一节 输入手写数字图片输出识别结果","level":"1.5.1","depth":2,"path":"chapter4/section4.1.md","ref":"chapter4/section4.1.md","articles":[]},"dir":"ltr"},"config":{"plugins":["katex","expandable-chapters-small","tbfed-pagefooter","alerts","copy-code-button","puml","graph","chart","klipse","donate","simple-page-toc","splitter"],"root":".","styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"pluginsConfig":{"tbfed-pagefooter":{"copyright":"Copyright &copy scottdu 2018","modify_label":"该文件修订时间：","modify_format":"YYYY-MM-DD HH:mm:ss"},"puml":{},"simple-page-toc":{"maxDepth":3,"skipFirstH1":true},"splitter":{},"search":{},"lunr":{"maxIndexSize":1000000,"ignoreSpecialCharacters":false},"graph":{},"donate":{"alipay":"http://ovhbzkbox.bkt.clouddn.com/2018-08-11-alipay1.jpeg","alipayText":"支付宝打赏","button":"赏","title":"","wechat":"http://ovhbzkbox.bkt.clouddn.com/2018-08-11-wechatpay.png","wechatText":"微信打赏"},"katex":{},"fontsettings":{"theme":"white","family":"sans","size":2},"highlight":{},"alerts":{},"expandable-chapters-small":{},"copy-code-button":{},"klipse":{"myConfigKey":"it's the default value"},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false},"chart":{"type":"c3"}},"theme":"default","author":"scottdu","pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"variables":{},"title":"Tensorflow学习笔记","language":"zh-hans","gitbook":"3.2.3","description":"记录Tensorflow的学习内容"},"file":{"path":"chapter4/section4.2.md","mtime":"2018-08-18T07:53:44.137Z","type":"markdown"},"gitbook":{"version":"3.2.3","time":"2018-09-25T05:55:22.995Z"},"basePath":"..","book":{"language":""}});
        });
    </script>
</div>

        
    <script src="../gitbook/gitbook.js"></script>
    <script src="../gitbook/theme.js"></script>
    
        
        <script src="../gitbook/gitbook-plugin-expandable-chapters-small/expandable-chapters-small.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-alerts/plugin.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-copy-code-button/toggle.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-donate/plugin.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-splitter/splitter.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-search/search-engine.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-search/search.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-lunr/lunr.min.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-lunr/search-lunr.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-sharing/buttons.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-fontsettings/fontsettings.js"></script>
        
    

    </body>
</html>

